package com.minhua.jijiehao2.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.text.InputType;
import android.text.method.DigitsKeyListener;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.minhua.jijiehao2.R;
import com.minhua.jijiehao2.base.adapter.ViewHolder;
import com.minhua.jijiehao2.constants.DefineKey;
import com.minhua.jijiehao2.utils.ActivityTaskManager;

/**
 * @author moo
 * @date 2017/11/20.
 */

public class TextEditItemView extends RelativeLayout {

    private ViewHolder viewHolder;
    private int rightImg;
    private int rightImgChange;

    public TextEditItemView(Context context) {
        this(context, null);
    }

    public TextEditItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextEditItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        viewHolder = ViewHolder.get(context, null, this, R.layout.layout_text_edit_item, 0, true);
        initItemView();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextEditItemView, defStyleAttr, 0);
        init(typedArray);
        typedArray.recycle();
    }

    /**
     * 基础界面初始化
     */
    private void initItemView() {
        viewHolder.getConvertView().setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick();
                }
            }
        });
    }

    private void init(TypedArray typedArray) {
        for (int i = 0; i < typedArray.getIndexCount(); i++) {
            int index = typedArray.getIndex(i);
            switch (index) {
                case R.styleable.TextEditItemView_ivLeftSrc:
                    int resourceId = typedArray.getResourceId(index, 0);
                    viewHolder.setVisible(R.id.ivLeft, VISIBLE);
                    viewHolder.setImageResource(R.id.ivLeft, resourceId);
                    break;
                case R.styleable.TextEditItemView_tvLeftText:
                    String leftText = typedArray.getString(index);
                    viewHolder.setVisible(R.id.tvLeft, VISIBLE);
                    viewHolder.setText(R.id.tvLeft, leftText);
                    break;
                case R.styleable.TextEditItemView_tvLeftTextColor:
                    int leftColor = typedArray.getColor(index, 0);
                    viewHolder.setVisible(R.id.tvLeft, VISIBLE);
                    viewHolder.setTextColor(R.id.tvLeft, leftColor);
                    break;
                case R.styleable.TextEditItemView_etHint:
                    String hint = typedArray.getString(index);
                    EditText editText = viewHolder.getView(R.id.etContent);
                    editText.setHint(hint);
                    break;
                case R.styleable.TextEditItemView_etTextType:
                    int anInt1 = typedArray.getInt(index, -1);
                    switch (anInt1) {
                        case 0:
                            ((EditText) viewHolder.getView(R.id.etContent)).setInputType(InputType.TYPE_CLASS_NUMBER);
                            break;
                        default:
                            break;
                    }
                    break;
                case R.styleable.TextEditItemView_tvCenterText:
                    viewHolder.setText(R.id.tvContent, typedArray.getString(index));
                    break;
                case R.styleable.TextEditItemView_tvRightText:
                    String rightText = typedArray.getString(index);
                    viewHolder.setVisible(R.id.tvRight, VISIBLE);
                    viewHolder.setText(R.id.tvRight, rightText);
                    break;
                case R.styleable.TextEditItemView_tvRightTextColor:
                    int rightColor = typedArray.getColor(index, 0);
                    viewHolder.setVisible(R.id.tvRight, VISIBLE);
                    viewHolder.setTextColor(R.id.tvRight, rightColor);
                    break;
                case R.styleable.TextEditItemView_ivRightSrc:
                    rightImg = typedArray.getResourceId(index, 0);
                    viewHolder.setVisible(R.id.ivRight, VISIBLE);
                    viewHolder.setImageResource(R.id.ivRight, rightImg);
                    break;
                case R.styleable.TextEditItemView_ivRightSrcChange:
                    rightImgChange = typedArray.getResourceId(index, 0);
                    break;
                case R.styleable.TextEditItemView_type:
                    int anInt = typedArray.getInt(index, 0);
                    initView(anInt);
                    break;
                case R.styleable.TextEditItemView_dividerLeft:
                    int left = typedArray.getInt(index, 0);
                    initDivider(left);
                    break;
                case R.styleable.TextEditItemView_centerGravity:
                    int centerGravity = typedArray.getInt(index, 0);
                    if (centerGravity == 1) {
                        ((EditText) viewHolder.getView(R.id.etContent)).setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
                        ((TextView) viewHolder.getView(R.id.tvContent)).setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
                    } else {
                        ((EditText) viewHolder.getView(R.id.etContent)).setGravity(Gravity.CENTER);
                        ((TextView) viewHolder.getView(R.id.tvContent)).setGravity(Gravity.CENTER);
                    }
                    break;
                default:
                    break;
            }
        }

    }

    private void initDivider(int left) {
        View divider = viewHolder.getView(R.id.divider);
        RelativeLayout.LayoutParams layoutParams = (LayoutParams) divider.getLayoutParams();
        switch (left) {
            case 0:
                layoutParams.addRule(ALIGN_LEFT, R.id.etContent);
                break;
            case 1:
                layoutParams.addRule(ALIGN_LEFT, R.id.llLeft);
                break;
            case 2:
                layoutParams.addRule(ALIGN_PARENT_LEFT);
                layoutParams.addRule(ALIGN_PARENT_RIGHT);
                break;
            case 3:
                divider.setVisibility(GONE);
                break;
            default:
                break;
        }
        divider.setLayoutParams(layoutParams);
    }

    public String getText() {
        return ((EditText) viewHolder.getView(R.id.etContent)).getText().toString().trim();
    }

    public void setText(String text) {
        ((EditText) viewHolder.getView(R.id.etContent)).setText(text);
    }

    public String getCenterText() {
        return ((TextView) viewHolder.getView(R.id.tvContent)).getText().toString().trim();
    }

    public void setCenterText(String text) {
        initTextView();
        ((TextView) viewHolder.getView(R.id.tvContent)).setText(text);
    }

    public ViewHolder getViewHolder() {
        return viewHolder;
    }

    public EditText getEditText() {
        return (EditText) viewHolder.getView(R.id.etContent);
    }

    public TextView getCenterTextView() {
        return (TextView) viewHolder.getView(R.id.tvContent);
    }

    private void initView(int anInt) {
        switch (anInt) {
            //phone
            case 0:
                initPhoneView();
                break;
            //password
            case 1:
                initPasswordView();
                break;
            //code
            case 2:
                initCodeView();
                break;
            //text显示
            case 3:
                initTextView();
                break;
            default:
                break;
        }
    }


    private void initTextView() {
        viewHolder.getView(R.id.etContent).setVisibility(GONE);
        viewHolder.getView(R.id.tvContent).setVisibility(VISIBLE);
    }

    private void initCodeView() {
        TextView tvRight = viewHolder.getView(R.id.tvRight);
        EditText code = viewHolder.getView(R.id.etContent);
        code.setInputType(InputType.TYPE_CLASS_NUMBER);
        InputFilter[] filters = {new InputFilter.LengthFilter(4)};
        code.setFilters(filters);
        tvRight.setText("获取验证码");
        tvRight.setVisibility(VISIBLE);

        tvRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCodeSendListener != null) {
                    onCodeSendListener.onStartCode();
                }
            }
        });
    }

    private int countDown = 60;
    @SuppressLint("HandlerLeak")
    private Handler updateHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            TextView tvRight = viewHolder.getView(R.id.tvRight);
            tvRight.setClickable(false);
            viewHolder.setText(R.id.tvRight, countDown-- + "s");
            if (countDown == -1) {
                tvRight.setClickable(true);
                tvRight.setText("重新发送");
                countDown = 60;
            } else {
                updateHandler.sendEmptyMessageDelayed(0, 1000);
            }
        }
    };

    public void startTimer() {
        updateHandler.sendEmptyMessage(0);
    }

    public void endTimer() {
        countDown = 60;
        updateHandler.removeCallbacksAndMessages(null);
    }


    private boolean isPassword;

    private void initPasswordView() {
        final EditText editText = viewHolder.getView(R.id.etContent);
        editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

        final ImageView ivRight = viewHolder.getView(R.id.ivRight);
        isPassword = true;
        editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        ivRight.setImageResource(rightImg);

        ivRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isPassword) {
                    editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    ivRight.setImageResource(rightImgChange);
                } else {
                    editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    ivRight.setImageResource(rightImg);
                }
                isPassword = !isPassword;
            }
        });
    }

    private void initPhoneView() {
        EditText phone = viewHolder.getView(R.id.etContent);
        InputFilter[] inputFilters = {new InputFilter.LengthFilter(11)};
        phone.setFilters(inputFilters);
        phone.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
    }

//    @Override
//    public void setOnClickListener(@Nullable OnClickListener l) {
//        viewHolder.setOnClickListener(R.id.rlItemView, l);
//    }

    private OnCodeSendListener onCodeSendListener;

    public void setOnCodeSendListener(OnCodeSendListener onCodeSendListener) {
        this.onCodeSendListener = onCodeSendListener;
    }

    public interface OnCodeSendListener {
        void onStartCode();
    }

    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick();
    }
}
